<template>
  <div class="add-menu">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm" size='large'>
      <el-form-item label="菜单名称" prop="title">
        <el-input v-model="ruleForm.title" />
      </el-form-item>
      <el-form-item label="上级菜单">
        <el-select v-model="ruleForm.parentId" placeholder="请选择">
          <el-option label="无上级菜单" :value="0" />
          <el-option v-for="item in parentList" :label="item.title" :value="item.id" :key="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="前端名称" prop="name">
        <el-input v-model="ruleForm.name" :disabled="ruleForm.id !== 0" />
      </el-form-item>
      <el-form-item label="前端图标">
        <el-input v-model="ruleForm.icon" :disabled="ruleForm.id !== 0" />
      </el-form-item>
      <el-form-item label="是否显示" prop="hidden">
        <!-- hidden 为1显示   为0不显示 -->
        <el-radio-group v-model="ruleForm.hidden">
          <el-radio :label="0">是</el-radio>
          <el-radio :label="1">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model.number="ruleForm.sort" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useRoute } from 'vue-router';
import * as http from '@/request/http';
import router from '@/router';

interface RuleForm {
  parentId?: number;
  id: number;
  name: string;
  title: string;
  icon: string;
  hidden: number;
  sort: number;
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  id: 0,
  name: '',
  title: '',
  icon: '',
  hidden: 0,
  sort: 0,
})

const route = useRoute();
// 拿到 路由query参数 id
const currentMenuId = parseInt(route.query.id as string);

// 上级菜单列表
const parentList = ref<IMenuModel[]>([]);

// 获取  上级菜单
http.menu.getMenuListById({
  pageNum: 1,
  pageSize: 100,
  parentId: 0,
}).then(res => {
  // console.log('上级菜单 :', res);
  parentList.value = res.data.list;
})

const queryMenu = () => {
  // 如果是点击 编辑菜单 才会通过query传参的方式传过来id
  // 所以如果这个 currentMenuId 存在的话,就发送请求去获取菜单详情
  if (currentMenuId) {
    http.menu.getMenuById(currentMenuId).then(res => {
      console.log('当前菜单详情 :', res);
      // 更换ruleForm表单的数据
      Object.assign(ruleForm, res.data);
    })
  }
}
queryMenu();

const rules = reactive<FormRules<RuleForm>>({
  title: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }],
  name: [{ required: true, message: '前端名称不能为空', trigger: 'blur' }],
})

// 点击 提交  进行表单校验
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      // alert('验证通过,提交成功!');
      // 还需要判断  当前是添加 还是 编辑(存在currentMenuId)
      if (currentMenuId) {
        // 编辑
        // console.log('编辑-ruleForm : ', ruleForm);
        // 发送请求 编辑当前菜单
        http.menu.updateMenu(ruleForm).then(res => {
          if (res.code === 200) {
            // 提示用户  添加菜单成功
            ElMessage.success('修改菜单成功');
            // 跳转回到 菜单列表页面
            router.push('/ums/menu');

          }
        })
      } else {
        // 添加
        // console.log('添加-ruleForm : ', ruleForm);
        // 发送请求 添加新的菜单
        http.menu.createMenu(ruleForm).then(res => {
          if (res.code === 200) {
            // 提示用户  添加菜单成功
            ElMessage.success('添加菜单成功');
            // 跳转回到 菜单列表页面
            router.push('/ums/menu');
          }
        })

      }
    } else {
      // alert('验证不通过,无法提交!');
      ElMessage.warning('请先填写表单!');
    }
  })
}


</script>


<style scoped lang="less">
.add-menu {
  padding: 20px;
  // box-shadow: 0 2px 12px 1px rgba(0, 0, 0, .1);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
</style>